
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_test_demo/pages/upload/VideoPlayPage.dart';
import 'package:flutter_test_demo/static/colors/MyColors.dart';
import 'package:photo_album_manager/album_model_entity.dart';
import 'package:photo_album_manager/photo_album_manager.dart';
import 'package:video_player/video_player.dart';

class UpLoadFile extends StatefulWidget {
  final String resoureType;
  UpLoadFile({Key key , @required this.resoureType}) : super(key: key);

  @override
  _UpLoadFileState createState() => _UpLoadFileState();
}

class _UpLoadFileState extends State<UpLoadFile> {
  List<AlbumModelEntity> photos = List();

  Future<void> initPlatformState() async{
    //获取本地图片
    if(widget.resoureType == "photo"){
      photos = await PhotoAlbumManager.getDescAlbumImg();
      // 更新视图
      setState(() { });
    }else if(widget.resoureType == "video"){
      // 本地图片
      photos = await PhotoAlbumManager.getDescAlbumVideo();
      setState(() { });
    }
  }

  @override
  void initState(){
    // TODO: implement initState
    super.initState();
    
    initPlatformState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget.resoureType == "photo" ? "相册" : "视频"
        ), 
        backgroundColor: MyColor.titleColor,
      ),
      body: GridView.builder(
        padding: EdgeInsets.all(10.0),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3,
          mainAxisSpacing: 10.0,
          crossAxisSpacing: 10.0
        ), 
        itemCount: photos.length,
        itemBuilder: (BuildContext context, int index){
          AlbumModelEntity model = photos[index];
          return GestureDetector(
            onTap: (){
              print(model.toJson());
              if(model.resourceType == "video" ){
                 Navigator.push(
                  context, 
                  MaterialPageRoute(builder: (context) => VideoPlayPage(url: File( model.originalPath)))
                );
              }

            },
            child: Card(
              child: Stack(
                children: <Widget>[
                  ConstrainedBox(
                    constraints: BoxConstraints.expand(),
                    child:  model.resourceType == "video" ? VideoPreview(videoAdress : File( model.originalPath)) : Image.file(
                      File(model.originalPath ?? model.thumbPath ),
                      fit: BoxFit.cover,
                    ),
                  ),
                  Offstage(
                    child: Center(
                      child: Icon(
                        Icons.play_circle_outline,
                        size: 40,
                        color: Colors.white,
                      ),
                    ),
                    offstage: model.resourceType == "video" ? false : true,
                  ),
                ]
              )
            )  
          );
        }
      ),
    );
  }
}



class VideoPreview extends StatefulWidget {
  final File videoAdress;
  VideoPreview({Key key, @required this.videoAdress}) : super(key: key);

  @override
  _VideoPreviewState createState() => _VideoPreviewState();
}

class _VideoPreviewState extends State<VideoPreview> {

  VideoPlayerController _controller;
  Future _initializeVideoPlayerFuture;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = VideoPlayerController.file(widget.videoAdress);
    _controller.setLooping(true);
    _initializeVideoPlayerFuture = _controller.initialize();
  }
  
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _controller.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _initializeVideoPlayerFuture,
      builder: (context, snapshot){
        print(snapshot.connectionState);

        if(snapshot.hasError){
          print(snapshot.error);
        }
        if(snapshot.connectionState == ConnectionState.done){
          return AspectRatio(aspectRatio: 1.0 ,child: VideoPlayer(_controller),);
        }
      },
    );
  }
}
